package cn.wyl.user.controller;

import cn.wyl.basic.exception.BusinessException;
import cn.wyl.user.dto.UserDto;
import cn.wyl.user.service.IUserService;
import cn.wyl.user.domain.User;
import cn.wyl.user.query.UserQuery;
import cn.wyl.basic.util.AxiousResult;
import cn.wyl.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    public IUserService userService;

    /**
     * 接口：添加或修改
     * @param user  传递的实体
     * @return AxiousResult 响应给前端
     */
    @PutMapping
    public AxiousResult addOrUpdate(@RequestBody User user){
        try {
            if( user.getId()!=null)
                userService.update(user);
            else
                userService.save(user);
            return new AxiousResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AxiousResult(false,"操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AxiousResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AxiousResult delete(@PathVariable("id") Long id){
        try {
            userService.remove(id);
            return new AxiousResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AxiousResult(false,"删除失败了");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AxiousResult 响应给前端
    */
    @PatchMapping
    public AxiousResult patchDelete(@RequestBody List<Long> ids){
        try {
                userService.patchDel(ids);
            return new AxiousResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AxiousResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public User findOne(@PathVariable("id")Long id) {
        return userService.loadById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<User> findAll(){
        return userService.loadAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<User> queryPage(@RequestBody UserQuery query) {
        return userService.queryPage(query);
    }

    /**
     * 前端：post('/user/register/phone',this.phoneUserForm)
     * phoneUserForm: {
     *                 phone:'',       //以防用户更改手机号，需要再次验证
     *                 imageCode: '', //获取手机验证码的时候已经校验过 不需要再校验了
     *                 phoneCode:'',
     *                 password:'',
     *                 confirmpassword:''
     *             }
     * 接口参数：UserDto
     * 接口返回值：AxiousResult
     */
    @PostMapping("/register/phone")
    public AxiousResult registerByPhone(@RequestBody UserDto userDto){
        try {
            userService.registerByPhone(userDto);
            return new AxiousResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AxiousResult(false,"注册失败!"+e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AxiousResult(false,"系统错误!"+e.getMessage());
        }
    }
}
